package com.steshov.trainer.service;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.steshov.trainer.R;
import com.steshov.trainer.activities.Index;
import com.steshov.trainer.utils.Reservation;

public class NotificationService extends Service {

	static ServiceActivity serviceActivity;
	static Intent intent;
	static Context context;

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		context = getApplicationContext();
		serviceActivity = new ServiceActivity();
		giveSignal(intent.getExtras().getString("exercise"),intent.getExtras().getInt("amount"));
		Reservation.put(context, "state", "success");
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	public static void giveSignal(String exercise, int amount) {
		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification note = new Notification(
				android.R.drawable.stat_notify_more, "Trainer calls!",
				System.currentTimeMillis());
		CharSequence title = "Trainer says:";
		CharSequence detail = "Do " + exercise + " " + amount + " times!";
		Intent intent = new Intent(context, Index.class);
		PendingIntent penIntent = PendingIntent.getActivity(context, 0, intent,
				0);
		note.sound = Uri.parse("android.resource://com.steshov.trainer"
				+ R.raw.notification);
		note.setLatestEventInfo(context, title, detail, penIntent);
		notificationManager.notify(0, note);
	}


}

class ServiceActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_blank);
		Log.i("====", "Creates?");
	}

	public ServiceActivity getThis() {
		Log.i("====", "returns?");
		return this;
	}
}
